package API_itextpdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * Classe que cria o cabeçalho do arquivo pdf.
 * 
 * @author PW-UFRN
 */
class TableHeader extends PdfPageEventHelper {
        
        /** Texto do cabeçalho */
        String header;
        /** The template with the total number of pages. */
        PdfTemplate total;
        
        /** Formatação do cabeçalho em tabela. */
        PdfPTable table;
        
        /** Variavel auxiliar para retirar o cabeçalho da capa */
        boolean aux;
        
        /**
         * Allows us to change the content of the header.
         * @param header The new header String
         */
        public void setHeader(String header, Boolean aux) {
            this.header = header;
            this.aux = aux;
        }

        /**
         * Creates the PdfTemplate that will hold the total number of pages.
         * @see com.itextpdf.text.pdf.PdfPageEventHelper#onOpenDocument(
         *      com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
         */
        @Override
        public void onOpenDocument(PdfWriter writer, Document document) {
            total = writer.getDirectContent().createTemplate(30, 16);
        }

        /**
         * Adds a header to every page
         * @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(
         *      com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
         */
        @Override
        public void onEndPage(PdfWriter writer, Document document) {
            table = new PdfPTable(3);
            try {
                    table.setWidths(new int[]{30, 8, 2});
                    table.setTotalWidth(527);
                    table.setLockedWidth(true);
                    table.getDefaultCell().setFixedHeight(20);
                    table.getDefaultCell().setBorder(Rectangle.BOTTOM);
                    table.addCell(header);
                    table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
                    table.addCell(String.format("Pagina %d de ", writer.getPageNumber()));
                    PdfPCell cell = new PdfPCell(Image.getInstance(total));
                    cell.setBorder(Rectangle.BOTTOM);
                    table.addCell(cell);
                if(!aux){
                    table.writeSelectedRows(0, -1, 34, 828, writer.getDirectContent());//writer.getDirectContent());
                }
            }
            catch(DocumentException de) {
                throw new ExceptionConverter(de);
            }
        }

        /**
         * Fills out the total number of pages before the document is closed.
         * @see com.itextpdf.text.pdf.PdfPageEventHelper#onCloseDocument(
         *      com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
         */
        @Override
        public void onCloseDocument(PdfWriter writer, Document document) {
            ColumnText.showTextAligned(total, Element.ALIGN_LEFT,
                    new Phrase(String.valueOf(writer.getPageNumber() - 1)),
                    2, 2, 0);
        }
    }
